//
// Created by fyt79 on 2019/6/9.
//

#include "thread.h"

pthread_t* start_thread(unsigned int core_id, unsigned int tpu_id, size_t stack_size, void* stack_address, void* (*f)(void*), void* arg) {
  struct sched_param* sched;
  pthread_attr_t* attr;
  pthread_t* thread;
  attr = attrs_cache + (core_id*MACH_THREADS + tpu_id);
  thread = thread_cache + (core_id*MACH_THREADS + tpu_id);
  sched = sched_cache + (core_id*MACH_THREADS + tpu_id);
  pthread_attr_init(attr);
  pthread_attr_setschedpolicy(attr, SCHED_PIN);
  sched->sched_priority = 0;
  sched->sched_cpuset = 0x01 << tpu_id;
  pthread_attr_setschedparam(attr, sched);
  pthread_attr_setcore_sb(attr, core_id);
  if (stack_size != 0) {
    pthread_attr_setstacksize(attr, stack_size);
  }
  if(stack_address != 0) {
    pthread_attr_setstackaddr(attr, stack_address);
  }
  pthread_create(thread, attr, f, arg);
  return thread;
}
